﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FPModel;

namespace FacebookPoster
{
    public class Pages
    {
        public static List<FBPage> Get()
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBPages.ToList();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static FBPage GetByID(int id)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBPages.FirstOrDefault(us => us.ID == id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }

        }

        public static FBPage GetByPageToken(string pageToken)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    return context.FBPages.FirstOrDefault(us => us.PageToken.ToLower() == pageToken.ToLower());
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

        public static int Add(FBPage page)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    context.AddToFBPages(page);
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        public static int Update(int id, string newName)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    FBPage pageEdit = new FBPage();
                    pageEdit = (from c in context.FBPages where c.ID == id select c).First();
                    pageEdit.PageName = newName;
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        public static int Delete(int id)
        {
            using (FBPosterEntities context = new FBPosterEntities())
            {
                try
                {
                    FBPage pageEdit = new FBPage();
                    pageEdit = (from c in context.FBPages where c.ID == id select c).First();

                    context.DeleteObject(pageEdit);
                    context.SaveChanges();

                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }
    }
}